﻿using AutoMapper;
using Factory.MasterData.MaterialClassifications;
using Factory.MasterData.Materials.Dto;
using Factory.MasterData.Vendors.Dto;
using Factory.MasterData.Vendors;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.MasterData.Vendors
{
    /// <summary>
    /// 服务接口
    /// </summary>
    public class VendorAppService : ApplicationService, IVendorAppService
    {
        /// <summary>
        /// 供应商仓储接口
        /// </summary>
        private readonly IRepository<Vendor, int> _vendorRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="vendorRepository">供应商仓储接口</param>
        /// <param name="mapper">映射</param>
        public VendorAppService(IRepository<Vendor, int> vendorRepository, IMapper mapper)
        {
            _vendorRepository = vendorRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加重复性验证
        /// </summary>
        /// <param name="code">供应商编码</param>
        /// <returns>返回结果</returns>
        public int AddRepeatability(string code)
        {
            long count = _vendorRepository.GetListAsync(m => m.Code == code).Result.Count();
            return count > 0 ? -1 : 0;
        }
        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="vendorDto">添加的供应商信息</param>
        /// <returns>返回结果</returns>
        public async Task<int> AddVender(VendorDto vendorDto)
        {
            var reslut = AddRepeatability(vendorDto.Code);
            if (reslut != 0) return -1;

            Vendor vendor = _mapper.Map<Vendor>(vendorDto);
            await _vendorRepository.InsertAsync(vendor);
            return 1;
        }
        /// <summary>
        /// 供应商信息分页显示
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="inquireName">供应商名称</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> GetVendorPage(int pageIndex, int pageSize, string? inquireName)
        {
            var list =await _vendorRepository.GetListAsync(m => string.IsNullOrEmpty(inquireName) || m.Name.Contains(inquireName));
            PageResult pageResult = new PageResult();
            pageResult.RowCount = list.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }
        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="id">供应商主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> DeleteVendor(string id)
        {
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                await _vendorRepository.DeleteAsync(m=>m.Id== Convert.ToInt32(item));
            }
            return 1;
        }
        /// <summary>
        /// 编辑重复性验证
        /// </summary>
        /// <param name="code">供应商编码</param>
        /// <param name="id">供应商主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> UpdateRepeatability(string code, int id)
        {
            var list =await _vendorRepository.GetListAsync(m => m.Code == code && m.Id != id);
            return list.Count() > 0 ? -1 : 0;
        }
        /// <summary>
        /// 编辑供应商信息
        /// </summary>
        /// <param name="vendorDto">编辑的供应商信息</param>
        /// <returns>返回结果</returns>
        public async Task<int> UpdateVender(VendorDto vendorDto)
        {
            Vendor vendor = _mapper.Map<Vendor>(vendorDto);
            await _vendorRepository.UpdateAsync(vendor);
            return 1;
        }
    }
}
